package com.tebiecloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.model.entity.BaseApi;
import com.tebiecloud.base.server.service.BaseApiService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 系统资源-API接口 前端控制器
 * @author tebie
 * @date 2019-08-02
 */
@RestController
@RequestMapping("baseapi")
public class BaseApiController {

    @Autowired
    private BaseApiService baseApiService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "/list")
    public ResultBody<IPage<BaseApi>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        BaseApi query = pageParams.mapToObject(BaseApi.class);
        QueryWrapper<BaseApi> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(baseApiService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ResultBody<BaseApi> get(@RequestParam("id") Long id) {
        BaseApi entity = baseApiService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param apiCode 接口编码,
     * @param apiName 接口名称,
     * @param apiCategory 接口分类:default-默认分类,
     * @param apiDesc 资源描述,
     * @param requestMethod 请求方式,
     * @param contentType 响应类型,
     * @param serviceId 服务ID,
     * @param path 请求路径,
     * @param priority 优先级,
     * @param status 状态:0-无效 1-有效,
     * @param isPersist 保留数据0-否 1-是 不允许删除,
     * @param isAuth 是否需要认证: 0-无认证 1-身份认证 默认:1,
     * @param isOpen 是否公开: 0-内部的 1-公开的,
     * @param className 类名,
     * @param methodName 方法名
     * @return
     */
    @PostMapping("/add")
    public ResultBody add(
        @RequestParam(value = "apiCode", required = false) String apiCode,
        @RequestParam(value = "apiName", required = false) String apiName,
        @RequestParam(value = "apiCategory", required = false) String apiCategory,
        @RequestParam(value = "apiDesc", required = false) String apiDesc,
        @RequestParam(value = "requestMethod", required = false) String requestMethod,
        @RequestParam(value = "contentType", required = false) String contentType,
        @RequestParam(value = "serviceId", required = false) String serviceId,
        @RequestParam(value = "path", required = false) String path,
        @RequestParam(value = "priority", required = false) Long priority,
        @RequestParam(value = "status", required = false) Integer status,
        @RequestParam(value = "isPersist", required = false) Integer isPersist,
        @RequestParam(value = "isAuth", required = false) Integer isAuth,
        @RequestParam(value = "isOpen", required = false) Integer isOpen,
        @RequestParam(value = "className", required = false) String className,
        @RequestParam(value = "methodName", required = false) String methodName
    ) {
        BaseApi entity = new BaseApi();
        entity.setApiCode(apiCode);
        entity.setApiName(apiName);
        entity.setApiCategory(apiCategory);
        entity.setApiDesc(apiDesc);
        entity.setRequestMethod(requestMethod);
        entity.setContentType(contentType);
        entity.setServiceId(serviceId);
        entity.setPath(path);
        entity.setPriority(priority);
        entity.setStatus(status);
        entity.setIsPersist(isPersist);
        entity.setIsAuth(isAuth);
        entity.setIsOpen(isOpen);
        entity.setClassName(className);
        entity.setMethodName(methodName);
        baseApiService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param apiCode 接口编码
     * @param apiName 接口名称
     * @param apiCategory 接口分类:default-默认分类
     * @param apiDesc 资源描述
     * @param requestMethod 请求方式
     * @param contentType 响应类型
     * @param serviceId 服务ID
     * @param path 请求路径
     * @param priority 优先级
     * @param status 状态:0-无效 1-有效
     * @param isPersist 保留数据0-否 1-是 不允许删除
     * @param isAuth 是否需要认证: 0-无认证 1-身份认证 默认:1
     * @param isOpen 是否公开: 0-内部的 1-公开的
     * @param className 类名
     * @param methodName 方法名
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(
            @RequestParam(value = "apiId", required = false) Long apiId,
            @RequestParam(value = "apiCode", required = false) String apiCode,
            @RequestParam(value = "apiName", required = false) String apiName,
            @RequestParam(value = "apiCategory", required = false) String apiCategory,
            @RequestParam(value = "apiDesc", required = false) String apiDesc,
            @RequestParam(value = "requestMethod", required = false) String requestMethod,
            @RequestParam(value = "contentType", required = false) String contentType,
            @RequestParam(value = "serviceId", required = false) String serviceId,
            @RequestParam(value = "path", required = false) String path,
            @RequestParam(value = "priority", required = false) Long priority,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "isPersist", required = false) Integer isPersist,
            @RequestParam(value = "isAuth", required = false) Integer isAuth,
            @RequestParam(value = "isOpen", required = false) Integer isOpen,
            @RequestParam(value = "className", required = false) String className,
            @RequestParam(value = "methodName", required = false) String methodName
    ) {
        BaseApi entity = new BaseApi();
        entity.setApiId(apiId);
        entity.setApiCode(apiCode);
        entity.setApiName(apiName);
        entity.setApiCategory(apiCategory);
        entity.setApiDesc(apiDesc);
        entity.setRequestMethod(requestMethod);
        entity.setContentType(contentType);
        entity.setServiceId(serviceId);
        entity.setPath(path);
        entity.setPriority(priority);
        entity.setStatus(status);
        entity.setIsPersist(isPersist);
        entity.setIsAuth(isAuth);
        entity.setIsOpen(isOpen);
        entity.setClassName(className);
        entity.setMethodName(methodName);
        baseApiService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("/remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            baseApiService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            baseApiService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
